import path from "path";
import fse from "fs-extra";
import { getYesterdayLogFileName, formatNow } from "../utils/index";
import { DIST_FOLDER_NAME } from "../config/const";

const splitLogFile = (accessLogPath: string) => {
  console.log(`----------- 拆分日志文件 开始 -----------`);
  console.log(`当前的时间`, formatNow());

  const accessLogFile = path.join(accessLogPath, `access.log`);

  const distFolder = path.join(accessLogPath, DIST_FOLDER_NAME);
  // 验证文件夹是否存在
  fse.ensureDirSync(distFolder);

  const distFile = path.join(distFolder, getYesterdayLogFileName());

  // 创建文件
  fse.ensureFileSync(distFile);
  fse.outputFileSync(distFile, "");

  // 文件拷贝
  fse.copySync(accessLogFile, distFile);

  // 清空access.log
  fse.outputFileSync(accessLogFile, "");

  console.log(`----------- 拆分日志文件 结束 -----------`);
};

export default splitLogFile;
